<template>
  <div>
    <tablePaging :tableItem="tableItem" :operationItem="operationItem"/>
  </div>
</template>

<script setup>
import tablePaging from '@/views/common/component/table-paging'
import { onMounted, reactive, ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {tableItem, operationItem, saleDisableApi, saleListApi} from './const.js'
import { ElMessage } from 'element-plus'
import { download } from "@/utils/request";
const router = useRouter()
const { proxy } = getCurrentInstance();
// 搜索
operationItem.menuArray[0].handle = () => {
  clickSearch()
}
// 导出
operationItem.menuArray[1].handle = () => {
  clickExport()
}
//禁用
tableItem.btnArray[0].handle = (row) => {
  switchEnableOrDisable(row)
}
//启用
tableItem.btnArray[1].handle = (row) => {
  switchEnableOrDisable(row)
}
function clickSearch() {
  tableItem.pageInfo.paging()
}
function clickExport() {
  let url = '/sale/management/export'
  let fileName = '商家统计表'
  download(url, tableItem.pageInfo.request, fileName, {}, {})
}
//禁用/启用
function switchEnableOrDisable(row) {
  let text = row.disabledFlag === 0 ? "禁用" : "启用";
  proxy.$modal.confirm('确认要' + text + '【' + row.sale + '】商家吗?').then(function () {
    let dataRequest = {
      id: row.id,
      disabledFlag: row.disabledFlag === 0? 1 : 0
    }
    return saleDisableApi(dataRequest)
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
    tableItem.pageInfo.paging()
  }).catch(() => { });
}
//设置销售搜索栏可选性
function setSaleRootOptionsList() {
  let dataRequest = {
    typeCode: 'root',
    disabledFlag: '0'
  }
  saleListApi(dataRequest).then(res => {
    const saleList = res.rows || res.data
    saleList.map(item => {
      operationItem.SearchItem[1].options.push({ label: item.sale, value: item.num })
    })
  })
}


onMounted(() => {
  setSaleRootOptionsList()
})

</script>
<style></style>
